package com.ruoyi.performance.service.impl;

import com.ruoyi.performance.domain.PerfDirectScore;
import com.ruoyi.performance.domain.PerfDitemScore;
import com.ruoyi.performance.mapper.PerfDirectScoreMapper;
import com.ruoyi.performance.mapper.PerfDitemScoreMapper;
import com.ruoyi.performance.service.PerfDirectScoreService;
import com.ruoyi.performance.service.PerfDitemScoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author YL
 * @date 2020/8/26 8:41
 * @description 绩效模板Service接口实现类
 */
@Service
public class PerfDitemScoreServiceImpl implements PerfDitemScoreService {

    @Resource
    private PerfDitemScoreMapper perfDitemScoreMapper;

    @Resource
    private PerfDirectScoreMapper perfDirectScoreMapper;

    /**
     * 查询细项得分
     *
     * @param perfDitemScore 细项ID
     * @return 细项得分
     */
    @Override
    public PerfDitemScore selectPerfDitemScoreById(PerfDitemScore perfDitemScore) {
        return perfDitemScoreMapper.selectPerfDitemScoreById(perfDitemScore);
    }

    /**
     * 获取考核单中细项总分
     *
     * @param checkId 考核单ID
     * @return 细项得分
     */
    @Override
    public int selectPerfDitemScoreByCheckId(int checkId) {
        int count;
        List<PerfDitemScore> perfDitemScores=perfDitemScoreMapper.selectPerfDitemScoreByCheckId(checkId);
        count = perfDitemScores.stream().mapToInt(PerfDitemScore::getScore).sum();
        return count;
    }

    /**
     * 新增细项得分
     *
     * @param perfDitemScore 细项得分
     * @return 结果
     */
    @Override
    public int insertPerfDitemScore(PerfDitemScore perfDitemScore) {
        //获取细项ID
        int ditemId=perfDitemScore.getDitemId();
        //获取细项得分
        int score=perfDitemScore.getScore();
        //根据细项ID获取该细项直接打分情况
        PerfDirectScore perfDirectScore = perfDirectScoreMapper.selectPerfDirectScoreById(ditemId);
        //直接打分分值上限
        int upperScore= perfDirectScore.getUpperScore();
        //直接打分分值下限
        int lowerScore= perfDirectScore.getLowerScore();
        if (score>upperScore||score<lowerScore){
            //打分超过分值范围
            return -1;
        }
        return perfDitemScoreMapper.insertPerfDitemScore(perfDitemScore);
    }

    /**
     * 修改细项得分
     *
     * @param perfDitemScore 细项得分
     * @return 结果
     */
    @Override
    public int updatePerfDitemScore(PerfDitemScore perfDitemScore) {
        return perfDitemScoreMapper.updatePerfDitemScore(perfDitemScore);
    }
}
